// pages/ucenter/auth/auth.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    userInfo: {},
    user_id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id: options.id
    });
  },

  bindGetUserInfo(e) {
    var that = this;
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('user_id');
    if (userInfo && token) {
      return;
    }
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      user.loginByWeixinxxc(e.detail).then(res => {
        this.setData({
          userInfo: res.userInfo,
          user_id: res.user_id
        });
        app.globalData.userInfo = res.userInfo;
        app.globalData.token = res.token;

        wx.request({
          url: api.getInviterUserInfo,
          data: {
            id: that.data.id,
            user_id: wx.getStorageSync('user_id')
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          method: "POST",
          success: function (res) {
            console.log('3' + res.data);

            if (res.data.is_inviter == false) {
              wx.showModal({
                title: '温馨提示',
                content: '您已经为该好友助力过啦',
                showCancel: false,
                confirmText: '我要参加',
                success: function (res) {
                  wx.navigateTo({
                    url: '/pages/share/share',
                  })
                }
              });
            } else {
              wx.request({
                url: api.addInviter,
                data: {
                  id: that.data.id,
                  userInfo: JSON.stringify(that.data.userInfo),
                  user_id: that.data.user_id,
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
                method: "POST",
                success: function (res) {
                  wx.showToast({
                    title: '助力成功',
                  });
                  //助力成功跳转
                  wx.navigateTo({
                    url: '/pages/share/share',
                  });
                }
              })
            }
          }
        });
        
      }).catch((err) => {
        console.log(err)
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告通知',
        content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) { ////如果用户重新同意了授权登录
                  user.loginByWeixin(e.detail).then(res => {
                    this.setData({
                      userInfo: res.data.userInfo
                    });
                    app.globalData.userInfo = res.data.userInfo;
                    app.globalData.token = res.data.token;
                  }).catch((err) => {
                    console.log(err)
                  });
                }
              }
            })
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})